package com.qf.ssm.common.handle;



import com.qf.ssm.common.exception.ServiceException;
import com.qf.ssm.common.vo.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * 先定义个全局异常处理类
 * 在类上使用 @RestControllerAdvice
 */
@RestControllerAdvice
@Slf4j
public class GlobalHandlerException {

    /**
     * @return
     */
    @ExceptionHandler(Exception.class)
    public static ResponseResult<Object> handlerException(Exception ex) {
        log.error(ex.getMessage());
        if (ex instanceof ServiceException) {
            ServiceException serviceException = (ServiceException) ex;
            return ResponseResult.error(serviceException.getResponseCode());
        }
        return ResponseResult.error();
    }

    @ExceptionHandler(ServiceException.class)
    public static ResponseResult<Object> handlerException(ServiceException ex) {
        log.error(ex.getMessage());
        return ResponseResult.error(ex.getResponseCode());
    }

}
